﻿//AcademicPlanner - course registration planning web app.
//Copyright (C) 2009  Boubacar Diallo

//This program is free software; you can redistribute it and/or
//modify it under the terms of the GNU General Public License
//as published by the Free Software Foundation; either version 2
//of the License, or (at your option) any later version.

//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.

//You should have received a copy of the GNU General Public License
//along with this program; if not, write to the Free Software
//Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.


using System;
using System.Collections.Generic;
using System.Web.Mvc;
using AcademicPlanner.Domain.Models;

namespace AcademicPlanner.Presentation.Views.Shared
{
	/// <summary>
	/// Possible scenarios of a <c>AcademicScheduleForm</c> form.
	/// </summary>
	/// <since>1.0</since>
	public enum AcademicScheduleFormScenario
	{
		CREATE,
		EDIT
	}


	/// <summary>
	/// AcademicScheduleFormModel represents a model object for a <c>AcademicScheduleForm</c> form.
	/// </summary>
	/// <since>1.0</since>
	public class AcademicScheduleFormModel : FormModel
	{
		/// <summary>
		/// Gets or sets the scenario to use the <c>AcademicScheduleForm</c> form under.
		/// </summary>
		/// <since>1.0</since>
		public AcademicScheduleFormScenario Scenario { get; set; }

		/// <summary>
		/// Gets or sets the <c>AcademicSchedule</c> whose data to save.
		/// </summary>
		/// <remarks>
		/// This property is not used in <c>CREATE</c> scenario.
		/// </remarks>
		/// <since>1.0</since>
		public AcademicSchedule Schedule { get; set; }

		/// <summary>
		/// Gets or sets the <c>AcademicPlan</c> the academic schedule belongs to.
		/// </summary>
		/// <remarks>
		/// This property is used in <c>CREATE</c> scenario only.
		/// </remarks>
		/// <since>1.0</since>
		public AcademicPlan AcademicPlan { get; set; }
	}


	/// <summary>
	/// AcademicScheduleForm represents a form control to save a <c>AcademicSchedule</c> data.
	/// </summary>
	/// <since>1.0</since>
	public partial class AcademicScheduleForm : Form<AcademicScheduleFormModel>
	{
		/// <summary>
		/// Gets the model object for this form control.
		/// </summary>
		/// <since>1.0</since>
		public new AcademicScheduleFormModel Model { get { return (AcademicScheduleFormModel)base.Model; } }

		/// <summary>
		/// Gets or sets the value of the name field of this form.
		/// </summary>
		/// <since>1.0</since>
		public string NameValue { get; set; }

		/// <summary>
		/// Gets or sets the value of the start year field of this form.
		/// </summary>
		/// <since>1.0</since>
		public string StartYearValue { get; set; }

		/// <summary>
		/// Gets or sets the list of options in the program dropdown of this form.
		/// </summary>
		/// <since>1.0</since>
		public IList<SelectListItem> ProgramOptions { get; set; }

		/// <summary>
		/// Gets or sets the value of the program field of this form.
		/// </summary>
		/// <since>1.0</since>
		public string ProgramValue { get; set; }

		/// <summary>
		/// Gets or sets the list of options in the start term dropdown of this form.
		/// </summary>
		/// <since>1.0</since>
		public IList<SelectListItem> StartTermOptions { get; set; }

		/// <summary>
		/// Gets or sets the value of the start term field of this form.
		/// </summary>
		/// <since>1.0</since>
		public string StartTermValue { get; set; }


		/// <summary>
		/// Handles the page load event for this form.
		/// </summary>
		/// <remarks>
		/// This methods intializes or customizes the form's properties
		/// based on the model object's <c>Scenario</c>.
		/// </remarks>
		/// <param name="sender">The sender of the event.</param>
		/// <param name="e">The details about the event.</param>
		/// <since>1.0</since>
		protected void Page_Load(object sender, EventArgs e)
		{
			method = FormMethod.Post;

			if (Model.Scenario == AcademicScheduleFormScenario.EDIT)
			{
				actionName = "Save";
				controllerName = "Schedules";
				routeValues = new { pId = Model.Schedule.EntityId };
				Title = Tr("Edit Schedule");
				Name = "editAcademicScheduleForm";
				SubmitButtonValue = Tr("Save");
			}
			else
			{
				actionName = "Save";
				controllerName = "Schedules";
				routeValues = new { pId = 0, pParentId = Model.AcademicPlan.EntityId };
				Title = Tr("Create Schedule");
				Name = "createAcademicScheduleForm";
				SubmitButtonValue = Tr("Save");
			}

			if (string.IsNullOrEmpty(Model.Id)) Model.Id = Name;

			if (Model.Scenario == AcademicScheduleFormScenario.EDIT)
			{
				NameValue = Model.Schedule.Name;
			}
			else
			{
				NameValue = "";
				ProgramValue = "";
				StartYearValue = "";
				StartTermValue = "";

				ProgramOptions = new List<SelectListItem>();
				foreach (var vProg in Model.AcademicPlan.Programs.ToList())
				{
					ProgramOptions.Add(new SelectListItem
					{
						Text = vProg.Name,
						Value = vProg.EntityId.ToString(),
						Selected = false
					});
				}
				StartTermOptions = new List<SelectListItem>();
				foreach (var vTerm in Model.AcademicPlan.Terms.ToList())
				{
					StartTermOptions.Add(new SelectListItem
					{
						Text = vTerm.Name,
						Value = vTerm.EntityId.ToString(),
						Selected = false
					});
				}
			}
		}
	}
}